<template>
  <div class="newVr">
    <div class="mask"></div>
    <div class="new_vr">
      <div class="vs-top">
        <p>{{ !$route.query.oid ? '新建资料（厂商退货）' : '厂商退货补单' }}</p>
        <i class="el-icon-close" @click="closeNewClerk"></i>
      </div>

      <div class="vs-info">
        <div class="vs-info-1">
          <p>退货日期</p>
          <el-date-picker
            v-model="inputDate"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            disabled
          >
          </el-date-picker>
          <p>厂商编号</p>
          <div class="infoBox">
            <el-input
              v-model="manufacturerNo"
              @blur="manufacturerNoBlur"
              :disabled="supplement"
            ></el-input>
            <el-select
              v-model="manufacturerName"
              @change="manufacturerNameChange"
              :disabled="supplement"
            >
              <el-option
                v-for="item in manufacturerList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
                <span style="float: left">{{ item.value }}</span>
                <span style="float: right">{{ item.label }}</span>
              </el-option>
            </el-select>
          </div>
          <p style="width: 70px">单号</p>
          <el-input v-model="odd" :disabled="supplement"></el-input>
          <p>厂退序号</p>
          <el-input v-model="inputSequence" disabled></el-input>
        </div>
        <div class="vs-info-2">
          <p>仓库</p>
          <div class="infoBox">
            <el-input
              v-model="companyNo"
              @blur="companyNoBlur"
              disabled
            ></el-input>
            <el-select
              v-model="companyName"
              @change="
                (e) => {
                  this.companyNo = e
                }
              "
              disabled
            >
              <el-option
                v-for="item in warehouseList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
                <span style="float: left">{{ item.value }}</span>
                <span style="float: right">{{ item.label }}</span>
              </el-option>
            </el-select>
          </div>

          <p>品牌</p>
          <el-select v-model="brand" :disabled="supplement">
            <el-option
              v-for="item in brandList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>

          <!-- <p class="t_content">品名</p>
          <el-select v-model="commodityName">
            <el-option
            v-for="item in commodityNameList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
            </el-option>
          </el-select> -->

          <p>配货方式</p>
          <el-select v-model="cooperateWay" :disabled="supplement">
            <el-option
              v-for="item in cooperateWayList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>

          <p>折数</p>
          <el-input v-model="discount" :disabled="supplement"></el-input>
        </div>

        <el-table
          id="codeTable"
          border
          :data="codeData"
          class="tb-edit"
          style="width: 100%; margin-bottom: 15px"
          max-height="148px"
          @cell-click="cellClick"
          :header-cell-style="{
            background: '#f4f4f4',
            padding: 0,
            height: '40px'
          }"
          :cell-style="{ padding: 0, height: '36px' }"
        >
          <el-table-column :label="header" width="150"> </el-table-column>
          <el-table-column prop="colorName" label="颜色" width="100">
          </el-table-column>

          <template v-for="i in sizeList">
            <el-table-column :prop="i" :label="i" :key="i + Math.random()">
              <template slot-scope="scope">
                <el-input
                  size="small"
                  class="innerInp"
                  v-model="scope.row[i]"
                  @blur="
                    handleInputBlurResult($event, {
                      index: scope.$index,
                      key: i,
                      result: scope.row[i]
                    })
                  "
                  @keyup.enter.native="inpEnter"
                >
                </el-input>
                <!-- <span>{{scope.row[i]}}</span> -->
              </template>
            </el-table-column>
          </template>
        </el-table>

        <div class="barcode">
          <p>条码扫描</p>
          <el-switch
            v-model="isBarcode"
            active-color="#409EFF"
            inactive-color="#C0C4CC"
            :disabled="creating"
          >
          </el-switch>
          <div>
            <p>条码选择</p>
            <el-radio
              :disabled="!isBarcode || creating"
              v-model="radio"
              label="1"
              >货号码</el-radio
            >
            <el-radio
              :disabled="!isBarcode || creating"
              v-model="radio"
              label="2"
              >ID码</el-radio
            >
          </div>
        </div>

        <div class="enterBarcode">
          <el-input
            v-model="barcode1"
            ref="barcodeInput"
            placeholder="扫描/输入货号或款号，输完按回车"
            @keyup.enter.native="barcodeEnter1($event)"
          >
          </el-input>
          <span>合计：{{ total }}</span>
        </div>

        <el-table
          height="210px"
          :data="barcodeData"
          style="width: 100%"
          :header-cell-style="{
            background: '#f4f4f4',
            padding: 0,
            height: '40px'
          }"
          :cell-style="{ padding: 0, height: '36px' }"
        >
          <el-table-column prop="kindsNumber" label="货号"> </el-table-column>
          <el-table-column prop="colorName" label="颜色"> </el-table-column>
          <el-table-column prop="sizeNo" label="尺码编号"> </el-table-column>
          <el-table-column prop="sum" label="数量"> </el-table-column>
          <el-table-column prop="onePrice" label="单价"> </el-table-column>
          <el-table-column prop="sellingPrice" label="售价"> </el-table-column>
          <el-table-column prop="sellingPrice" label="厂价" width="100">
            <template slot-scope="scope">
              <el-input
                size="small"
                v-model="scope.row.manufacturerPrice"
                @blur="mpBlur(scope.row)"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="commodityName" label="品名"> </el-table-column>
        </el-table>
      </div>

      <div class="vs-btn">
        <el-button @click="closeNewClerk">关闭</el-button>
        <!-- <el-button plain type="primary" @click="saveNew_vr">保存</el-button> -->
        <el-button type="primary" @click="continueInput">继续录入</el-button>
      </div>

      <!-- F7弹出框 -->
      <div class="popup" :style="{ display: showPopup }">
        <div class="popup_t">
          <p>输入扫描数量</p>
          <i @click="closePopup" class="el-icon-close"></i>
        </div>
        <div class="popup_c">
          <p>请输入扫描数量</p>
          <p>如果不输入则扫描数量为1</p>
          <el-input
            v-model="scanNum"
            style="margin-top: 10px"
            @keyup.enter="determine"
          ></el-input>
          <!-- <input type="text" 
            class="popup_button" 
            v-model="scanNum"
            @keyup.enter="determine"> -->
        </div>
        <div class="popup_b">
          <el-button @click="closePopup">取消</el-button>
          <el-button type="primary" @click="determine">确定</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getManufacturer,
  getBarCode,
  factoryOut,
  renewRepertory,
  outQuery,
  updateMPR
} from '../../../../network/manufacturer'
import { getKindsNumberList, searchQuery } from '../../../../network/artNo'
import { getSize } from '../../../../network/size'
import { getBranded } from '../../../../network/branded'

import exportMethods from '../../../../components/mixins/exportMethods'

export default {
  mixins: [exportMethods],
  props: {
    sendData: {
      type: Object,
      default: {}
    },
    supplementList: {
      type: Array,
      default: []
    },
    flag: {
      type: String,
      default: 'none'
    },
    Str: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      showPopup: 'none',
      isEdit: true,
      inputDate: '',
      manufacturerNo: '',
      manufacturerName: '',
      manufacturerList: [],
      odd: '',
      inputSequence: '',
      warehouseList: [],
      companyNo: '',
      companyName: '',
      brand: '',
      brandList: [],
      cooperateWay: '配货',
      cooperateWayList: [
        { value: '配货', label: '配货' },
        { value: '订货', label: '订货' },
        { value: '订断', label: '订断' },
        { value: '追断', label: '追断' }
      ],
      discount: 10,
      header: '',
      sizeList: ['7', '9', '1', '3', '5', '7', 'F'],
      codeData: [],
      creating: false,
      isBarcode: false,
      radio: '1',
      styleNumber: '',
      colorNumber: '',
      code: '',
      name: '',
      nameList: [],
      vendorPrice: '',
      barcodeStyle: '',
      color: '',
      colorList: [],
      barcode1: '',
      barcode2: '',
      barcodeData: [],
      tmpBarcodeData: [],

      // 条码规律详情
      manufacturerBarCode: {},

      kindsNumberMap: {},

      tmpData: {},
      scanNum: 1,
      beforeScanNum: 1,
      supplement: false,
      total: 0
    }
  },
  watch: {
    sendData(a, b) {
      this.inputSequence = a.inputSequence
      this.inputDate = a.inputDate
      this.manufacturerNo = a.manufacturerNo
      this.manufacturerName = a.manufacturerName
      this.manufacturerList = a.manufacturerList
      this.odd = a.odd
      this.warehouseList = a.warehouseList
      this.companyNo = a.companyNo
      this.companyName = a.companyName
      this.brand = a.brand
      this.brandList = a.brandList
      this.cooperateWay = a.cooperateWay
      this.$forceUpdate()
    },
    flag(a, b) {
      if (a == 'block') {
        this.total = 0
        if (this.creating) this.creating = false
        if (this.$route.query.oid) {
          this.supplement = true
          outQuery(
            JSON.stringify({
              sequence: this.$route.query.oid,
              pageData: {
                page: 1,
                limit: 999
              }
            })
          ).then((res) => {
            let obj = res.data.data.resultList[0]
            this.discount = obj.discount
            this.barcodeData = []
            this.total = 0
            this.kindsNumberMap = {}
            obj.detailVOList.forEach((e) => {
              // e.enteringTime = e.enteringTime + ' 00:00:00'
              // e.purchaseTime = e.purchaseTime + ' 00:00:00'
              e.knInfo = e.kindsNumber
              e.kindsNumber = e.knInfo.kindsNumber
              e.colorName = e.knInfo.colorName
              e.colorNo = e.knInfo.colorNo
              e.size = e.knInfo.size
              e.sizeNo = e.knInfo.sizeNo
              e.commodityName = e.knInfo.commodityName
              e.brandedName = e.knInfo.brandedName
              e.oldMP = e.manufacturerPrice

              this.barcodeData.unshift(e)
              this.kindsNumberMap[e.kindsNumber] = e

              this.total += Number(e.sum)
            })
            this.barcodeData.sort((a, b) => {
              return a.kindsNumber.localeCompare(b.kindsNumber)
            })
          })
        } else {
          this.supplement = false
          this.inputSequence = ''
        }
      }
    },
    barcode1(a, b) {
      this.barcode1 = a.toUpperCase().trim()
    },
    manufacturerNo(a, b) {
      this.manufacturerNo = a.toUpperCase().trim()
    },
    companyNo(a, b) {
      this.companyNo = a.toUpperCase().trim()
    },
    odd(a, b) {
      this.odd = a.toUpperCase().trim()
    }
  },
  methods: {
    closeNewClerk() {
      this.Str = ''
      this.$route.query.oid = ''
      this.$emit('saveNew_vr', this.inputSequence)
      this.header = ''
      this.codeData = []
      this.barcodeData = []
      this.total = 0
      this.originalData = {}
      this.$emit('closeNewClerk')
    },
    sibling(e) {
      let s = []
      let pp = e.parentNode.parentNode.children
      for (let j = 0; j < pp.length; j++) {
        let p = pp[j].children
        for (let i = 0, pl = p.length; i < pl; i++) {
          if (p[i] !== e) s.push(p[i])
        }
      }
      return s
    },

    // 保存
    saveNew_vr() {},

    // 继续录入
    continueInput() {
      // 新增单号不能为空
      // if (!this.odd) {
      //   this.$alert('单号不能为空', '', {
      //     confirmButtonText: '确定'
      //   })
      //   return
      // }
      if (!this.inputDate) {
        this.$alert('日期不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!this.manufacturerNo) {
        this.$alert('厂商不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!this.companyNo) {
        this.$alert('仓库不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (this.discount == '') {
        this.$alert('折数不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }

      let checkParams = {}
      let kindsNumberNumberMap = {}

      // 之前扫描录入1条
      // console.log(this.tmpData)
      if (this.isBarcode) {
        if (this.barcode1 == '') return
        // 厂商退货更新虚拟库存
        kindsNumberNumberMap[this.tmpData.kindsNumber] = this.tmpData.sum

        if (JSON.stringify(kindsNumberNumberMap) == '{}') {
          this.$alert('无数据无法继续录入', '', {
            confirmButtonText: '确定'
          })
          return
        }

        checkParams = {
          manufacturerNo: this.manufacturerNo,
          companyNo: this.companyNo,
          kindsNumberNumberMap
        }
        // console.log(checkParams)
        if (this.radio == 2 && this.isBarcode) {
          this.outSave(kindsNumberNumberMap)
        }
        if (this.radio != 2 && this.isBarcode) {
          renewRepertory(JSON.stringify(checkParams)).then((res) => {
            console.log(res)
            if (res.data.code == 200 && res.data.data != null) {
              this.$message.warning({ message: '该货号库存不足', offset: 400 })
              return
            }
            if (res.data.code == 200) {
              this.outSave(kindsNumberNumberMap)
            } else if (res.data.code == 201) {
              this.$alert('请重新输入数量', '库存不足', {
                confirmButtonText: '确定'
              })
            }
          })
        }
        return
      }
      // 之前输入款号录入多条
      // console.log(this.codeData)
      let knList = []
      this.codeData.forEach((e) => {
        let c = e.colorNo
        this.sizeList.forEach((f) => {
          if (this.manufacturerBarCode.styleColorSize) {
            // 型尺色

            if (this.manufacturerBarCode.styleColor) {
              // 款号与尺码之间有 -

              if (this.manufacturerBarCode.colorSize) {
                // 色号与尺码之间有 -
                knList.push({
                  kindsNumber: `${this.header}-${f}-${c}`,
                  sum: e[f]
                })
              } else {
                // 色号与尺码之间没有 -
                knList.push({
                  kindsNumber: `${this.header}-${f}${c}`,
                  sum: e[f]
                })
              }
            } else {
              // 款号与尺码之间没有 -

              if (this.manufacturerBarCode.colorSize) {
                // 色号与尺码之间有 -
                knList.push({
                  kindsNumber: `${this.header}${f}-${c}`,
                  sum: e[f]
                })
              } else {
                // 色号与尺码之间没有 -
                knList.push({
                  kindsNumber: `${this.header}${f}${c}`,
                  sum: e[f]
                })
              }
            }
          } else {
            // 型色尺

            if (this.manufacturerBarCode.styleColor) {
              // 款号与颜色之间有 -

              if (this.manufacturerBarCode.colorSize) {
                // 色号与尺码之间有 -
                knList.push({
                  kindsNumber: `${this.header}-${c}-${f}`,
                  sum: e[f]
                })
              } else {
                // 色号与尺码之间没有 -
                knList.push({
                  kindsNumber: `${this.header}-${c}${f}`,
                  sum: e[f]
                })
              }
            } else {
              // 款号与颜色之间没有 -

              if (this.manufacturerBarCode.colorSize) {
                // 色号与尺码之间有 -
                knList.push({
                  kindsNumber: `${this.header}${c}-${f}`,
                  sum: e[f]
                })
              } else {
                // 色号与尺码之间没有 -
                knList.push({
                  kindsNumber: `${this.header}${c}${f}`,
                  sum: e[f]
                })
              }
            }
          }
        })
      })
      // console.log(knList)

      for (const k of knList) {
        for (const l of this.tmpBarcodeData) {
          // console.log(l)
          // console.log(k)
          // if (k.kindsNumber == l.kindsNumber) {
          //   l.sum = k.sum
          // }
        }
        if (k.sum > 0) {
          kindsNumberNumberMap[k.kindsNumber] = k.sum
        }
      }
      // console.log(this.tmpBarcodeData)

      if (JSON.stringify(kindsNumberNumberMap) == '{}') {
        this.$alert('无数据无法继续录入', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (this.isBarcode == true && this.radio == 2) return
      checkParams = {
        manufacturerNo: this.manufacturerNo,
        companyNo: this.companyNo,
        kindsNumberNumberMap
      }
      console.log(checkParams)
      renewRepertory(JSON.stringify(checkParams)).then((res) => {
        console.log(res)
        if (res.data.code == 200) {
          if (!this.creating) this.creating = true
          let tmpList = ''
          if (res.data.data) {
            tmpList = String(res.data.data)
            this.exportRaw(tmpList, '库存不足.txt')
            kindsNumberNumberMap = {}
            for (const k of knList) {
              if (k.sum > 0 && !tmpList.includes(k.kindsNumber)) {
                kindsNumberNumberMap[k.kindsNumber] = k.sum
              }
            }
          }

          let isExist = 0
          for (const g of this.tmpBarcodeData) {
            g.colorNoName = g.colorNo + '.' + g.colorName
            g.oldMP = g.manufacturerPrice
            if (!Number(g.sum)) continue
            if (
              !this.barcodeData.length &&
              tmpList.indexOf(g.kindsNumber) == -1
            ) {
              this.barcodeData.push(g)
              continue
            }
            for (const h of this.barcodeData) {
              if (
                h.kindsNumber == g.kindsNumber &&
                tmpList.indexOf(g.kindsNumber) == -1
              ) {
                h.sum = Number(h.sum) + Number(g.sum)
                isExist = 0
                break
              }
              isExist = 1
            }
            if (isExist && tmpList.indexOf(g.kindsNumber) == -1) {
              this.barcodeData.push(g)
            }
          }

          this.outSave(kindsNumberNumberMap)
          this.$forceUpdate()

          this.barcode1 = ''
          this.header = ''
          this.codeData = []
        } else if (res.data.code == 201) {
          this.$alert('请重新输入数量', '库存不足', {
            confirmButtonText: '确定'
          })
        }
      })
    },

    outSave(kindsNumberNumberMap) {
      // 扫描货号或输入货号，isKindsNumber为true
      let isKindsNumber = !(this.isBarcode && this.radio == 2)

      let rfidList = []
      if (isKindsNumber) {
      } else {
        kindsNumberNumberMap = {}
        for (const key in this.kindsNumberMap) {
          if (this.kindsNumberMap.hasOwnProperty(key)) {
            const element = this.kindsNumberMap[key]
            kindsNumberNumberMap[key] = Number(element.sum)
          }
        }
        rfidList = [{ showCode: this.barcode1 }]
      }

      // if (this.$route.query.oid) {
      if (this.inputSequence) {
        // 补单
        for (const key in this.kindsNumberMap) {
          if (this.kindsNumberMap.hasOwnProperty(key)) {
            for (const k in kindsNumberNumberMap) {
              if (kindsNumberNumberMap.hasOwnProperty(k)) {
                if (key == k) {
                  // if (this.isBarcode && this.radio == 2) {
                  //   this.kindsNumberMap[key].sum = Number(this.kindsNumberMap[key].sum) + Number(this.scanNum)
                  // } else {
                  //   this.kindsNumberMap[key].sum = Number(this.kindsNumberMap[key].sum) + Number(kindsNumberNumberMap[k])
                  // }
                  // delete kindsNumberNumberMap[k]
                  // kindsNumberNumberMap[k] = 0
                  break
                }
              }
            }
          }
        }
      }

      let d = {
        manufacturerDTO: {
          dataSource: this.radio == 1 ? 1 : 0,
          brandedName: this.brand,
          isNewBox: true,
          isKindsNumber,
          inputSequence: this.inputSequence,
          inputDate: this.inputDate,
          manufacturerNo: this.manufacturerNo,
          odd: this.odd,
          inputWarehouseNo: this.companyNo,
          companyNo: this.companyNo,
          cooperateWay: this.cooperateWay,
          kindsNumberNumberMap,
          kindsNumberMap: this.kindsNumberMap,
          discount: this.discount
        },
        rfidList,
        flagToken: this.Str
      }

      console.log(d)
      factoryOut(JSON.stringify(d)).then((res) => {
        if (res.data.code == 200) {
          console.log(res)
          this.barcode1 = ''
          this.inputSequence =
            res.data.data.totalDTO.manufacturerDTO.inputSequence
          this.kindsNumberMap = {}
          outQuery(
            JSON.stringify({
              sequence: this.inputSequence,
              pageData: {
                page: 1,
                limit: 999
              }
            })
          ).then((res) => {
            if (!res.data.data) {
              this.inputSequence = ''
              this.total = 0
              this.barcodeData = []
              return
            }
            let obj = res.data.data.resultList[0]
            this.total = 0
            this.barcodeData = obj.detailVOList

            this.barcodeData.forEach((e) => {
              e.knInfo = e.kindsNumber
              e.kindsNumber = e.knInfo.kindsNumber
              e.colorName = e.knInfo.colorName
              e.colorNo = e.knInfo.colorNo
              e.size = e.knInfo.size
              e.sizeNo = e.knInfo.sizeNo
              e.commodityName = e.knInfo.commodityName
              e.brandedName = e.knInfo.brandedName
              e.oldMP = e.manufacturerPrice

              this.kindsNumberMap[e.kindsNumber] = e
              this.total += Number(e.sum)
            })

            this.barcodeData.sort((a, b) => {
              return a.kindsNumber.localeCompare(b.kindsNumber)
            })
            // this.saveNew_vr()
          })
        } else {
          this.$message.error('保存失败')
        }
      })
    },

    // 输入货号按回车
    barcodeEnter1(e) {
      if (this.barcode1 != '') {
        if (this.isBarcode) {
          if (this.manufacturerNo == '') {
            this.$alert('厂商不能为空', '', {
              confirmButtonText: '确定'
            })
            return
          }
        }

        if (!this.isBarcode) {
          // 手输款号
          getKindsNumberList({ kindsNumberStr: this.barcode1 }).then((res) => {
            if (res.data.data.data.length) {
              this.header = res.data.data.data[0].styleNumber

              this.$emit('inpNumChange')
              // 品牌公司查条码规律详情
              getBarCode({
                company: res.data.data.data[0].brandedCompany
              }).then((res) => {
                this.manufacturerBarCode = res.data.data[0]
              })

              // console.log(this.manufacturerBarCode)
              // 输入款号显示中间表和尺码信息   ZZZ-AAS
              res.data.data.data.sort((a, b) => {
                return a.relevanceSize.localeCompare(b.relevanceSize)
              })
              let tmp = res.data.data.data
              this.tmpBarcodeData = tmp
              // console.log(tmp)

              // 中间表  尺码表头
              this.sizeList = [tmp[0].size]
              let isExistSize = 0
              for (const i of tmp) {
                for (const j of this.sizeList) {
                  if (j == i.size) {
                    isExistSize = 0
                    break
                  }
                  isExistSize = 1
                }
                if (isExistSize) {
                  this.sizeList.push(i.size)
                }
              }

              // 中间表
              let tmpCodeData = [
                { colorNo: tmp[0].colorNo, colorName: tmp[0].colorName }
              ]
              let isExistColor = 0
              for (const i of tmp) {
                for (const j of tmpCodeData) {
                  if (j.colorName == i.colorName) {
                    isExistColor = 0
                    break
                  }
                  isExistColor = 1
                }
                if (isExistColor) {
                  tmpCodeData.push({
                    colorNo: i.colorNo,
                    colorName: i.colorName
                  })
                }
              }
              tmpCodeData.forEach((e) => {
                this.sizeList.forEach((f) => {
                  e[f] = ''
                })
              })
              this.codeData = tmpCodeData
              res.data.data.data.forEach((m) => {
                m.onePrice = ((m.sellingPrice * m.discount) / 10).toFixed(2)
              })
              let codeTable = document.getElementById('codeTable')
              let inputs = codeTable.getElementsByTagName('input')
              // console.log(inputs)
              let int = setInterval(() => {
                clearInterval(int)
                inputs[0].focus()
                inputs[inputs.length - 1].onkeyup = (e) => {
                  if (window.event.keyCode == 13) {
                    this.continueInput()
                    inputs[inputs.length - 1].onkeyup = () => {}
                  }
                }
              }, 100)
            } else {
              this.$message('此货号/款号无数据')
              this.$refs.barcodeInput.focus()
            }
          })
        } else {
          // 扫描条码
          let params = {}
          if (this.radio == 1) {
            // 扫描货号码
            params = {
              // 是否条码扫描货号码
              isKindsNumber: true,
              // 是否开启条码扫描
              isScanner: true,
              searchValue: this.barcode1
            }
          } else if (this.radio == 2) {
            // 扫描ID码
            params = {
              // 是否条码扫描货号码
              isKindsNumber: false,
              // 是否开启条码扫描
              isScanner: true,
              searchValue: this.barcode1,
              flagToken: this.Str
            }
          }
          console.log(params)
          searchQuery(params).then((res) => {
            console.log(res)
            if (res.data.code == 200) {
              this.tmpData = res.data.data[0]
              this.tmpData.sum = this.scanNum
              this.continueInput()
              this.$refs.barcodeInput.focus()
            }
          })
        }
      }
    },

    //  输入回车/失去焦点统计
    //  index 行索引
    //  result 输入内容
    handleInputBlurResult(e, { index, key, result }) {
      e.target.parentNode.parentNode.parentNode.classList.remove('activeCell')
      let r = /^(0|[1-9][0-9]*|-[1-9][0-9]*)$/
      if (result != 0) {
        result = result.replace(/\b(0+)/gi, '')
      }
      if (!r.test(result) && result) {
        this.$alert('数量请输入整数', '', {
          confirmButtonText: '确定'
        })
        this.codeData[index][key] = 0
        return
      }
      this.codeData[index][key] = result
    },
    cellClick(row, column, cell, event) {
      // console.log(row)  //当行的全部数据
      // console.log(column)  //当列的全部数据
      // console.log(cell)  //点击格的html代码  td>div
      // console.log(event)  //MouseEvent
      // console.log(cell.innerHTML)

      // console.log(cell.children[0].children[0].children)
      // cell.children[0].children[0].children[0].focus()
      cell.classList.add('activeCell')
      let s = this.sibling(cell)
      // console.log(s)
      s.forEach((e) => {
        e.classList.remove('activeCell')
      })
    },
    inpEnter(e) {
      console.log(e)
      e.target.parentNode.parentNode.parentNode.classList.remove('activeCell')
    },

    closePopup() {
      this.showPopup = 'none'
      this.scanNum = this.beforeScanNum
    },
    // 扫描数量确定
    determine() {
      this.showPopup = 'none'
    },

    // 厂商编号失去焦点
    manufacturerNoBlur() {
      this.brandList = []

      if (this.manufacturerNo != '') {
        for (const i of this.manufacturerList) {
          if (this.manufacturerNo == i.value) {
            this.manufacturerName = i.label

            let brandedCompany = ''
            // 厂商编号查找品牌公司
            getManufacturer({ no: this.manufacturerNo, limit: 99999 }).then(
              (res) => {
                // console.log(res)
                for (const i of res.data.data) {
                  if (i.no == this.manufacturerNo) {
                    brandedCompany = i.brandedCompany
                    break
                  }
                }
                // 品牌公司查品牌
                getBranded({ brandedCompany, limit: 99999 }).then((res) => {
                  // console.log(res)
                  let num = 1
                  res.data.data.forEach((e) => {
                    this.brandList.push({ value: e.name, label: e.name })
                    if (num) {
                      if (!this.brand) this.brand = e.name
                      num = 0
                    }
                  })
                })
              }
            )
            return
          }
        }
        this.$alert('无此厂商编号', '', {
          confirmButtonText: '确定'
        })
        this.manufacturerNo = ''
        this.manufacturerName = ''
      } else {
        this.manufacturerName = ''
      }
    },
    // 选择厂商
    manufacturerNameChange(e) {
      this.manufacturerNo = e
      for (let b of this.manufacturerList) {
        if (e == b.value) {
          this.discount = b.discount
        }
      }
      this.manufacturerNoBlur()
    },
    companyNoBlur() {
      if (this.companyNo != '') {
        for (const i of this.warehouseList) {
          if (this.companyNo == i.value) {
            this.companyName = i.label
            return
          }
        }
        this.$alert('无此仓库编号', '', {
          confirmButtonText: '确定'
        })
        this.companyNo = ''
        this.companyName = ''
      } else {
        this.companyName = ''
      }
    },

    mpBlur(row) {
      const price = row.manufacturerPrice.toString().trim()
      let r = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/
      if (!r.test(price) && price != '0') {
        this.$message.error('金额格式不正确')
        return
      }
      if (row.oldMP == price) return
      // console.log('kkkkkkkkkkkk');
      // return
      updateMPR({
        kindsNumber: row.kindsNumber,
        price,
        sequence: row.sequence
      }).then((res) => {
        if (res.data.code != 200) {
          this.$message.error('厂价修改失败')
          row.manufacturerPrice = row.oldMP
        } else {
          row.oldMP = row.manufacturerPrice
        }
      })
    }
  },
  mounted() {
    let _this = this
    document.onkeydown = function (event) {
      let e = event || window.event || arguments.callee.caller.arguments[0]
      if (e && e.keyCode == 118) {
        // 按 F7
        if (_this.showPopup == 'none') {
          _this.beforeScanNum = _this.scanNum
          _this.showPopup = 'block'
        } else {
          _this.showPopup = 'none'
        }
      }
    }
  }
}
</script>

<style scoped>
/* 新建资料 */
.mask {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: #000;
  opacity: 0.3;
  z-index: 9;
}
.new_vr {
  position: fixed;
  top: 7%;
  left: 50%;
  transform: translateX(-50%);
  width: 1070px;
  max-height: 800px;
  z-index: 99;
  background-color: white;
  overflow: auto;
}
.new_vr .vs-top {
  height: 40px;
  background-color: #f8f8f8;
}
.new_vr .vs-top p {
  float: left;
  line-height: 40px;
  padding-left: 20px;
  font-weight: 700;
}
.new_vr .vs-top i {
  float: right;
  line-height: 40px;
  padding: 0 16px;
}

.new_vr input {
  height: 30px;
  border: solid 1px #dcdfe6;
  border-radius: 5px;
  margin-left: 10px;
}
.new_vr input:focus {
  outline: none;
  border: solid 1px #409eff;
}
.new_vr .vs-info-2 .inp-2 {
  margin-right: 15px;
}
/* .vs-info-1,.vs-info-2{
    
  } */
.vs-info {
  padding: 0 15px;
  padding-bottom: 10px;
  /* border-bottom: solid #f2f2f2 2px; */
}
.vs-info-1 p,
.vs-info-2 p {
  display: inline-block;
  width: 80px;
  text-align: right;
  margin-right: 15px;
}
.vs-info-1 p:nth-child(3) {
  margin-left: 10px;
}
.new_vr .vs-info-1 .inp-2 {
  margin-right: 43px;
}
.vs-info .vs-info-2 {
  margin-bottom: 15px;
}
.vs-info .barcode p {
  display: inline;
  margin-right: 15px;
}
.vs-info .barcode div {
  margin-top: 15px;
}

.colorBox,
.infoBox {
  display: inline;
}

.enterBarcode {
  height: 60px;
  background-color: #f2f2f2;
  margin: 15px 0;
}

.vs-btn {
  float: right;
  margin: 0 15px 15px;
}

.popup {
  width: 400px;
  height: 200px;
  border: 2px solid #f4f4f4;
  box-shadow: 0 2px 2px 2px #dbdbdb;
  z-index: 999;
  position: absolute;
  left: 50%;
  top: 50%;
  margin: -100px 0 0 -200px;
  background: white;
  display: block;
}
.popup_t {
  display: flex;
  justify-content: space-between;
  width: 100%;
  height: 40px;
  background: rgb(244, 244, 244);
  padding: 8px 15px;
}
.popup_t p {
  font-weight: 700;
}
.popup_c {
  padding: 8px 15px;
}
.popup_button {
  margin: 5px 0 0 0;
  width: 94%;
  height: 30px;
  padding: 0 10px;
  border: 2px solid #f4f4f4;
  border-radius: 3px;
}
.popup_b {
  margin: 10px 0 0 0;
  padding-left: 230px;
}
</style>

<style>
.new_vr .vs-info-1 .el-input,
.new_vr .vs-info-2 .el-input {
  /* margin: 10px 20px 0 10px; */
  margin-top: 10px;
  width: 160px;
}
.new_vr .el-input__inner {
  height: 30px !important;
}
.new_vr .el-input__icon {
  line-height: 30px !important;
}
.vs-info .barcode .el-switch {
  margin: 0 !important;
}

.new_vr .enterBarcode .el-input {
  width: 486px;
  margin: 14px 17px;
}

.new_vr .colorBox .el-input {
  width: 60px;
  margin-right: 10px;
}
.new_vr .vs-info-1 .infoBox .el-input,
.new_vr .vs-info-2 .infoBox .el-input {
  margin: 0 10px 0 0;
  width: 60px;
}
.new_vr .vs-info-1 .infoBox .el-input--suffix,
.new_vr .vs-info-2 .infoBox .el-input--suffix {
  width: 90px;
}
.new_vr .colorBox .el-input--suffix {
  width: 90px;
}
</style>
